package cn.com.yiqi.wsc.service.task;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.appparam.entity.AppParamEntity;
import cn.com.yiqi.wsc.dao.task.ScheduleJobDao;
import cn.com.yiqi.wsc.entity.task.ScheduleJob;
import cn.com.yiqi.wsc.service.sysparams.AppParamService;
import cn.com.yiqi.wsc.service.sysparams.dataobject.AppParamDO;
import cn.com.yiqi.wsc.service.weixin.WeixinPayService;
import cn.com.yiqi.wsc.util.CronUtil;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 何刚
 * @version V1.0
 * @Title: BirthDayRemainderTask
 * @Package cn.com.yiqi.wsc.service.task
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/3/24
 */


@Component("birthDayRemainderTask")
public class BirthDayRemainderTask {

    private static final String JOB_NAME = "birthday_remainder";
    private static final String GROUP_NAME = "customer";


    @Autowired
    private ScheduleJobDao scheduleJobDao;

    @Autowired
    private JobTaskService jobTaskService;


    @Autowired
    private WeixinPayService weixinPayService;

    @Autowired
    private AppParamService appParamService;

    /**
     * 返回生日提醒任务的ID
     *
     * @return
     */
    private Long getJobIdByJobNameAndGroupName() {

        return scheduleJobDao.getJobIdByJobNameAndGroupName(GROUP_NAME, JOB_NAME);
    }


    public void updateReminderTask(int h, int m) {

        String cron = CronUtil.h2Cron(h, m);

        Long jobId = getJobIdByJobNameAndGroupName();
        if (jobId == null) {
            //新增并开启
            ScheduleJob scheduleJob = new ScheduleJob();

            scheduleJob.setBeanClass("cn.com.yiqi.wsc.service.task.BirthDayRemainderTask");
            scheduleJob.setJobGroup(GROUP_NAME);
            scheduleJob.setJobName(JOB_NAME);
            scheduleJob.setJobStatus(ScheduleJob.STATUS_RUNNING);
            scheduleJob.setCronExpression(cron);
            scheduleJob.setIsConcurrent(ScheduleJob.CONCURRENT_IS);
            scheduleJob.setDescription("生日提醒");
            scheduleJob.setSpringId("birthDayRemainderTask");
            scheduleJob.setMethodName("remainder");

            jobTaskService.addTask(scheduleJob);
            try {
                jobTaskService.addJob(scheduleJob);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }

        } else {
            try {
                jobTaskService.updateCron(jobId, cron);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }

    }


    /**
     * qutarz调用方法
     */
    public void remainder() {

        AppParamEntity appParamEntity = new AppParamEntity();
        appParamEntity.setCode("BIRTHDAY");
        Result result = appParamService.getMarryAppParamList(appParamEntity);

        if (ResultUtil.isSuccess(result)) {
            AppParamDO paramDO = (AppParamDO) result.getData().get("appParam");
            String birthdayAdvanceDays = paramDO.getBirthdayAdvanceDays();

            if (birthdayAdvanceDays != null) {

                Integer days = Integer.parseInt(birthdayAdvanceDays.trim());

                weixinPayService.sendBirthdayMention(days);

            } else {
                throw new RuntimeException("【生日提醒】提前天数 无效");
            }

        } else {
            throw new RuntimeException("获取【生日提醒】天数失败");
        }

    }

}
